use std::any::Any;
use std::error::Error;
use bytes::BytesMut;
use rpc::router::{DoRouter, RouterFactory};
use rpc::{DefaultLocalSeqCreator, RpcContext, RpcFilter, RpcResult};
use rpc::bootstrap::BootstrapBuilder;
use rpc::codec::RpcEncode;
use rpc::error::RpcError;
use rpc_client::client::RpcClientBuilder;

#[test]
fn client_builder_test()-> Result<(), Box<dyn Error>> {
    let address = format!("127.0.0.1:{}", 5000);
    let bootstrap = BootstrapBuilder::new()
        .filter("/auth/*", Box::new(LoginFilter))
        .router("/login", Box::new(RouterFactoryDemo))
        .build();
    let rpc_client = RpcClientBuilder::new(bootstrap, &address)
        .set_current_codec_type("json")
        .build();

    let router = rpc_client.get_router("/login");
    assert!(router.is_some());

    let filters = rpc_client.get_filters();
    assert_eq!(filters.len(), 1);

    Ok(())
}

struct LoginFilter;

impl RpcFilter for LoginFilter {
    fn after(&self, rpc_channel: &mut RpcContext, data: &mut dyn Any) -> Result<(), RpcError> {

        Ok(())
    }
}

struct DoRouterDemo;
impl DoRouter for DoRouterDemo {
    fn execute(&mut self, rpc_context: &mut RpcContext) -> RpcResult<Box<dyn RpcEncode>> {
        Ok(Box::new(32i32))
    }
}
struct RouterFactoryDemo;
impl RouterFactory for RouterFactoryDemo {
    fn parse(&self, _context: &RpcContext, _bytes_mut: BytesMut) -> RpcResult<Box<dyn DoRouter>> {
        Ok(Box::new(DoRouterDemo {}))
    }
}